package com.bbx.taxi.client.Activity.Menu;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import butterknife.InjectView;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.base.BaseRequest;
import com.bbx.api.sdk.model.passanger.MyOrder;
import com.bbx.api.sdk.model.passanger.ShareDetailBuild;
import com.bbx.api.sdk.model.passanger.SharelinkBuild;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.model.passanger.Return.ShareDetail;
import com.bbx.api.sdk.model.passanger.Return.ShareStat;
import com.bbx.api.sdk.model.passanger.Return.Sharelink;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrders;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet;
import com.bbx.api.sdk.net.passeger.conn.ShareDetailNet;
import com.bbx.api.sdk.net.passeger.conn.ShareStatNet;
import com.bbx.api.sdk.net.passeger.conn.SharelinkNet;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Order.OrderEvaluationActivity2;
import com.bbx.taxi.client.Activity.login.WelcomeActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Extra.RecommenMsg;
import com.bbx.taxi.client.Util.AppStarted;
import com.bbx.taxi.client.widget.pop.SharePopView;
import com.bbx.taxi.client.widget.pop.SharePopView.OnShareDetaiFinish;
import com.example.loadview.LoadingDialog;
import com.umeng.socialize.bean.RequestType;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.StatusCode;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

/**
 * 推荐有奖
 * 
 */
public class RecommenActivity extends BaseBBXActivity implements OnShareDetaiFinish{
	@InjectView(R.id.layout_bg)
	RelativeLayout layout_bg;
	@InjectView(R.id.btn_share)
	Button btn_share;
	@InjectView(R.id.pop_background)
	View pop_background;
	@InjectView(R.id.tv_obtained)
	TextView tv_obtained;
	@InjectView(R.id.tv_coupon)
	TextView tv_coupon;
	@InjectView(R.id.tv_prompt)
	TextView tv_prompt;
	private ShareStatTask mShareStatTask;
	public String sum="0";
	public String totalsum="0";
	public String totalcnt="0";
	private LoadingDialog dialog;
	/**分享POP*/
	public SharePopView mSharePopView;

	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_recommen);
		super.onCreate(savedInstanceState);
		
	}

	@Override
	protected void initView() {
		setTitle(getString(R.string.recommen_award));
		dialog = new LoadingDialog(context);
		layout_bg.setBackgroundDrawable(new BitmapDrawable(
				getImageFromAssetsFile("bg_hb.png")));
		btn_share.setOnClickListener(this);
		tv_obtained.setOnClickListener(this);
		tv_obtained.setText(Html.fromHtml("已获得<font color=\"#169bdb\">" + 0
				+ "</font>元>>"));
		getShareStat();
		mSharePopView=new SharePopView(context,pop_background);
		mSharePopView.setOnShareDetaiFinish(this);
		mSharePopView.getShareDetai();
	}
	@Override
	protected void onResume() {
		Token token = SharedPreEncryptUtil.getStringEncryptValue(this,
				SharedPreEncryptUtil.TokenAndUid, "");
		if (token == null || token.equals("")) {
			mApplication.setUid("");
			mApplication.setToken("");
		} else {
			mApplication.setUid(token.uid);
			mApplication.setToken(token.access_token);
		}
		super.onResume();
	}

	@Override
	protected void initData() {
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_share:
			mSharePopView.onShow(v);
			break;
		case R.id.tv_obtained:
			startActivity(new Intent(this, RecommenContentActivity.class).putExtra(RecommenMsg.totalsum, totalsum).putExtra(RecommenMsg.totalcnt, totalcnt));
			break;
		default:
			super.onClick(v);
			break;
		}

	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler =mSharePopView.getController().getConfig().getSsoHandler(
				requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

	/**
	 * 从Assets中读取图片
	 */
	private Bitmap getImageFromAssetsFile(String fileName) {
		Bitmap image = null;
		AssetManager am = getResources().getAssets();
		try {
			InputStream is = am.open(fileName);
			image = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return image;

	}

	/**
	 * 获取获取分享统计信息
	 */
	public void getShareStat() {
		ShareDetailBuild mShareDetailBuild = new ShareDetailBuild(context);
		mShareDetailBuild.uid = mApplication.getUid();
		mShareDetailBuild.access_token = mApplication.getToken();
		mShareDetailBuild.type = Value.RecommenType;
		mShareStatTask = new ShareStatTask(context, mShareDetailBuild);
		mShareStatTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	/**
	 * 获取获取分享统计信息
	 */
	private class ShareStatTask extends BaseAsyncTask {

		private ShareDetailBuild mShareDetailBuild;

		public ShareStatTask(Activity context, ShareDetailBuild mShareDetailBuild) {
			super(context,false);
			this.mShareDetailBuild = mShareDetailBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				ShareStat mShareStat=(ShareStat)object;
				if(mShareStat!=null){
					totalsum=mShareStat.getTotalsum();
					totalcnt=mShareStat.getTotalcnt();
					totalsum=totalsum!=null?totalsum:"0";
					totalcnt=totalcnt!=null?totalcnt:"0";
					tv_obtained.setText(Html.fromHtml("已获得<font color=\"#169bdb\">" + totalsum
							+ "</font>元>>"));
				}

			}else{
				ToastUtil.showToast(context, R.string.no_network);
				finish();
			}
			if (dialog != null) {
				dialog.onDismiss();
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			if (dialog != null) {
				dialog.onDismiss();
			}
			ToastUtil.showToast(context, R.string.no_network);
			finish();
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mShareDetailBuild).getJson();
			return new ShareStatNet(context, json);
		}
	}
	protected void onDestroy() {
		if (mShareStatTask != null
				&& mShareStatTask.getStatus() != AsyncTask.Status.FINISHED)
			mShareStatTask.cancel(true);
		super.onDestroy();

	}

	@Override
	public void onShareDetaiFinish(String sum) {
		this.sum=sum;
		tv_coupon.setText("￥"+sum);
		tv_prompt.setText(sum);
		if(sum.equals("0")){
		}else{
		}
	}
}
